# coding: utf-8
import os
import sys
import traceback

from PySide6.QtCore import QUrl
from PySide6.QtGui import QDesktopServices

from ..signal_bus import signalBus


def isWin11():
    return sys.platform == 'win32' and sys.getwindowsversion().build >= 22000


def openUrl(url: str):
    if not url.startswith("http"):
        if not os.path.exists(url):
            return False

        QDesktopServices.openUrl(QUrl.fromLocalFile(url))
    else:
        QDesktopServices.openUrl(QUrl(url))

    return True


def exception_hook(exception: BaseException, value, tb):
    """ exception callback function """
    message = '\n'.join([''.join(traceback.format_tb(tb)),
                         '{0}: {1}'.format(exception.__name__, value)])
    signalBus.appErrorSignal.emit(message)
